package com.springbootmybatis.controller;

import com.alibaba.fastjson.JSONObject;
import com.springbootmybatis.entity.Dept;
import com.springbootmybatis.service.DeptService;
import com.springbootmybatis.utils.JSONResultHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

    private static Logger logger = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    DeptService deptServiceImpl;

    /**
     * 查询部门列表
     * @return
     */
    @RequestMapping("/query-list")
    private String queryList(){
        logger.info("/query-list");
        JSONObject jsonObject = new JSONObject();
        try {
            List<Dept> deptList = deptServiceImpl.getDeptList();
            JSONResultHandler.handlerObject(jsonObject, "0", "查询成功", deptList);
        } catch (Exception e) {
            logger.info("系统异常，e={}", e);
            JSONResultHandler.handlerObject(jsonObject, "-9001", "系统异常");
        }
        return jsonObject.toString();
    }

    /**
     * 添加部门信息
     * @param dept
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestBody Dept dept){
        logger.info("/add, dept={}", dept);
        JSONObject jsonObject = new JSONObject();
        try {
            if (dept == null){
                return JSONResultHandler.handler(jsonObject, "-1001", "参数不能为空");
            }
            deptServiceImpl.add(dept);
            JSONResultHandler.handlerObject(jsonObject, "0", "添加成功");
        } catch (Exception e) {
            logger.info("系统异常，e={}", e);
            JSONResultHandler.handlerObject(jsonObject, "-9001", "系统异常");
        }
        return jsonObject.toString();
    }

    /**
     * 更新部门信息
     * @param dept
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@RequestBody Dept dept){
        logger.info("/update, dept={}", dept);
        JSONObject jsonObject = new JSONObject();
        try {
            if (dept == null){
                return JSONResultHandler.handler(jsonObject, "-1001", "参数不能为空");
            }
            deptServiceImpl.update(dept);
            JSONResultHandler.handlerObject(jsonObject, "0", "更新成功");
        } catch (Exception e) {
            logger.info("系统异常，e={}", e);
            JSONResultHandler.handlerObject(jsonObject, "-9001", "系统异常");
        }
        return jsonObject.toString();
    }

    /**
     * 通过id删除部门信息
     * @param id
     * @return
     */
    @RequestMapping("/delete-by-id")
    public String deleteById(Integer id){
        logger.info("/delete-by-id, id={}", id);
        JSONObject jsonObject = new JSONObject();
        try {
            if (id == null){
                return JSONResultHandler.handler(jsonObject, "-1001", "参数不能为空");
            }
            deptServiceImpl.deleteById(id);
            JSONResultHandler.handlerObject(jsonObject, "0", "删除成功");
        } catch (Exception e) {
            logger.info("系统异常，e={}", e);
            JSONResultHandler.handlerObject(jsonObject, "-9001", "系统异常");
        }
        return jsonObject.toString();
    }
}
